﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.ServiceProcess;
using System.Threading;
using CrossPlatform;

namespace ControlCenterTools
{
    public partial class NetControlToolsServiceController : UserControl
    {
        /// <summary>
        /// Tools for communicating with the Service.
        /// </summary>
        /// <see cref="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx"/>
        public NetControlToolsServiceController()
        {
            InitializeComponent();
        }

        public enum SimpleServiceCustomCommands
        { StopWorker = 128, RestartWorker, CheckWorker };
        //static void Main(string[] args)
        void codePastedFromExampleListedInClassDocumentationAbove()
        {
            ServiceController[] scServices;
            scServices = ServiceController.GetServices();

            foreach (ServiceController scTemp in scServices)
            {

                if (scTemp.ServiceName == "Simple Service")
                {
                    // Display properties for the Simple Service sample
                    // from the ServiceBase example.
                    ServiceController sc = new ServiceController("Simple Service");
                    Console.WriteLine("Status = " + sc.Status);
                    Console.WriteLine("Can Pause and Continue = " + sc.CanPauseAndContinue);
                    Console.WriteLine("Can ShutDown = " + sc.CanShutdown);
                    Console.WriteLine("Can Stop = " + sc.CanStop);
                    if (sc.Status == ServiceControllerStatus.Stopped)
                    {
                        sc.Start();
                        while (sc.Status == ServiceControllerStatus.Stopped)
                        {
                            Thread.Sleep(1000);
                            sc.Refresh();
                        }
                    }
                    // Issue custom commands to the service
                    // enum SimpleServiceCustomCommands 
                    //    { StopWorker = 128, RestartWorker, CheckWorker };
                    sc.ExecuteCommand((int)SimpleServiceCustomCommands.StopWorker);
                    sc.ExecuteCommand((int)SimpleServiceCustomCommands.RestartWorker);
                    sc.Pause();
                    while (sc.Status != ServiceControllerStatus.Paused)
                    {
                        Thread.Sleep(1000);
                        sc.Refresh();
                    }
                    Console.WriteLine("Status = " + sc.Status);
                    sc.Continue();
                    while (sc.Status == ServiceControllerStatus.Paused)
                    {
                        Thread.Sleep(1000);
                        sc.Refresh();
                    }
                    Console.WriteLine("Status = " + sc.Status);
                    sc.Stop();
                    while (sc.Status != ServiceControllerStatus.Stopped)
                    {
                        Thread.Sleep(1000);
                        sc.Refresh();
                    }
                    Console.WriteLine("Status = " + sc.Status);
                    String[] argArray = new string[] { "ServiceController arg1", "ServiceController arg2" };
                    sc.Start(argArray);
                    while (sc.Status == ServiceControllerStatus.Stopped)
                    {
                        Thread.Sleep(1000);
                        sc.Refresh();
                    }
                    Console.WriteLine("Status = " + sc.Status);
                    // Display the event log entries for the custom commands
                    // and the start arguments.
                    System.Diagnostics.EventLog el = new System.Diagnostics.EventLog("Application");
                    System.Diagnostics.EventLogEntryCollection elec = el.Entries;
                    foreach (System.Diagnostics.EventLogEntry ele in elec)
                    {
                        if (ele.Source.IndexOf("SimpleService.OnCustomCommand") >= 0 |
                            ele.Source.IndexOf("SimpleService.Arguments") >= 0)
                            Console.WriteLine(ele.Message);
                    }
                }
            }


        }

        ServiceController m_NetControlService = null;
        ServiceController NetControlService
        {
            get
            {
                // if we don't have our service controller yet, get one
                if (m_NetControlService == null)
                {
                    ServiceController[] scServices;
                    scServices = ServiceController.GetServices();

                    foreach (ServiceController scTemp in scServices)
                    {
                        if (scTemp.ServiceName == CrossPlatform.CrossPlatformConstants.NetControlToolServiceName)
                        {
                            // Create our service controller.
                            //m_NetControlService = new ServiceController(CrossPlatform.CrossPlatformConstants.NetControlToolServiceName);
                            m_NetControlService = scTemp;
                            break;
                        }
                    }
                }
                else
                {
                    m_NetControlService.Refresh();
                }
                return m_NetControlService;
            }
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            if (NetControlService != null)
            {
                labelCurrentStatus.Text = "Service Status: " + NetControlService.Status;
            }
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            if (NetControlService != null)
            {
                if (NetControlService.Status == ServiceControllerStatus.Stopped)
                {
                    NetControlService.Start();
                    while ((NetControlService.Status == ServiceControllerStatus.Stopped)
                        || (NetControlService.Status == ServiceControllerStatus.StartPending))
                    {
                        Thread.Sleep(1000);
                        NetControlService.Refresh();
                    }
                }
                labelCurrentStatus.Text = "Service Status: " + NetControlService.Status;
            }
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            if (NetControlService != null)
            {
                if (NetControlService.Status == ServiceControllerStatus.Running)
                {
                    NetControlService.Stop();
                    while ((NetControlService.Status == ServiceControllerStatus.Running)
                        || (NetControlService.Status == ServiceControllerStatus.StopPending))
                    {
                        Thread.Sleep(1000);
                        NetControlService.Refresh();
                    }
                }
                labelCurrentStatus.Text = "Service Status: " + NetControlService.Status;
            }
        }
    }
}
